package com.animee.jiujiangdict;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.animee.jiujiangdict.adapter.PinyinGridAdapter;
import com.animee.jiujiangdict.bean.PinyinWordBean;
import com.animee.jiujiangdict.db.DBManager;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.util.ArrayList;
import java.util.List;
/* 拼音搜索和部首搜索界面公共的功能都封装到此activity当中*/
public class BaseSearchActivity extends BaseActivity {
    ExpandableListView exLv;
    PullToRefreshGridView refreshGv;
    TextView titleTv;
    int selGroupPos = 0,selChildPos=0;   //选中的组的位置，选中的组当中的item的位置
    int totalpage;   //总页数
    int page = 1,pagesize=48;  //获取的网址时第几页，默认一页获取多少条数据
    String word = "";
    List<PinyinWordBean.ResultBean.ListBean> gridDatas;   //右侧的gridView的数据源，先声明出来
    PinyinGridAdapter gridAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_pinyin);
        exLv = findViewById(R.id.searchpy_elv);
        refreshGv = findViewById(R.id.searchpy_pgv);
        titleTv = findViewById(R.id.searchpy_tv_title);
        initData();
        exLv.expandGroup(0);   //默认展开第一组
        // 设置GridView监听器
        setGVListener();

    }
    /* 设置GridView的监听器*/
    public void setGVListener() {
        refreshGv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);

        // 点击每一项，跳转到对应的详细解释的界面
        refreshGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取正在被点击的位置的对象
                PinyinWordBean.ResultBean.ListBean bean = gridDatas.get(position);
                String zi = bean.getZi();
                Intent intent = new Intent(getBaseContext(), WordInfoActivity.class);
                intent.putExtra("zi",zi);
                startActivity(intent);
            }
        });
    }
    /** 将获取到的数据写入到数据库当中，为了不会造成ANR，使用子线程，完成操作*/
    public void writeDBByThread(final List<PinyinWordBean.ResultBean.ListBean> list){
        new Thread(new Runnable() {
            @Override
            public void run() {
                DBManager.insertListToPywordtb(list);
            }
        }).start();
    }
    /*
     * 网络数据加载成功时，会调用的方法
     * */
    @Override
    public void onSuccess(String json) {
        // 解析数据
        PinyinWordBean bean = new Gson().fromJson(json, PinyinWordBean.class);
        PinyinWordBean.ResultBean resultBean = bean.getResult();
        totalpage = resultBean.getTotalpage();   //获取总页数
        List<PinyinWordBean.ResultBean.ListBean> list = resultBean.getList();   //获取的数据集合
        //将加载到的数据写入到数据库
        writeDBByThread(list);
        refreshDataByGV(list);
    }


    /*
     * 更新GridView当中的数据，提示适配器重新加载
     * */
    public void refreshDataByGV(List<PinyinWordBean.ResultBean.ListBean> list) {
        if (page == 1) {   //加载的新的拼音对应的集合
            gridDatas.clear();
            gridDatas.addAll(list);
            gridAdapter.notifyDataSetChanged();
        } else {
            gridDatas.addAll(list);
            gridAdapter.notifyDataSetChanged();
            //加载完成停止刷新
            refreshGv.onRefreshComplete();
        }
    }
    /** 因为GridView的item布局相同，数据源也相同，所以适配器使用同一个，这里先初始化GridView的数据*/
    public void initData() {
        gridDatas = new ArrayList<>();  //  初始化GridView的数据源
        gridAdapter = new PinyinGridAdapter(this, gridDatas);
        refreshGv.setAdapter(gridAdapter);
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.searchpy_iv_back:
                finish();
                break;
        }
    }


}
